import { menuToRoutes, eachTreeData } from 'ele-admin-pro';
import { HOME_PATH, LAYOUT_PATH, REDIRECT_PATH } from '@/config/setting';
import EleLayout from '@/layout/index.vue';
import RedirectLayout from '@/components/RedirectLayout';
const modules = import.meta.glob('/src/views/**/index.vue');

/**
 * 静态路由
 */
export const routes = [
  {
    path: '/login',
    component: () => import('@/views/login/index.vue'),
    meta: { title: '登录' }
  },
  {
    path: '/forget',
    component: () => import('@/views/forget/index.vue'),
    meta: { title: '忘记密码' }
  },
  // 404
  {
    path: '/:path(.*)*',
    component: () => import('@/views/exception/404/index.vue')
  },
  // 如果需要静态路由显示顶栏和侧边栏需要这样配置
  {
    path: '/user/profile',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '个人中心' },
    children: [
      {
        path: '/user/profile',
        component: () => import('@/views/user/profile/index.vue'),
        meta: {
          title: '个人中心'
        }
      }
    ]
  },
  {
    path: '/purchase/receipts_purchase',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '采购单' },
    children: [
      {
        path: '/purchase/receipts_purchase/detail',
        component: () =>
          import('@/views/purchase/receipts_purchase/detail/index.vue'),
        meta: {
          title: '采购单详情',
          active: '/purchase/receipts_purchase/get_list'
        }
      },
      {
        path: '/purchase/receipts_purchase/add',
        component: () =>
          import('@/views/purchase/receipts_purchase/add/index.vue'),
        meta: {
          title: '添加货品',
          active: '/purchase/receipts_purchase/get_list'
        }
      },
      {
        path: '/purchase/receipts_purchase/edit',
        component: () =>
          import('@/views/purchase/receipts_purchase/edit/index.vue'),
        meta: {
          title: '编辑货品',
          active: '/purchase/receipts_purchase/get_list'
        }
      }
    ]
  },
  {
    path: '/purchase/receipts_distribute',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '分配单' },
    children: [
      {
        path: '/purchase/receipts_distribute/detail',
        component: () =>
          import('@/views/purchase/receipts_distribute/detail/index.vue'),
        meta: {
          title: '分配单详情',
          active: '/purchase/receipts_distribute/get_list'
        }
      }
    ]
  },
  {
    path: '/purchase/receipts_stock',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '分配到货' },
    children: [
      {
        path: '/purchase/receipts_stock/detail',
        component: () =>
          import('@/views/purchase/receipts_stock/detail/index.vue'),
        meta: {
          title: '分配到货详情',
          active: '/purchase/receipts_stock/get_list'
        }
      }
    ]
  },
  {
    path: '/purchase/receipts_back_receive',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '返仓接收' },
    children: [
      {
        path: '/purchase/receipts_back_receive/detail',
        component: () =>
          import('@/views/purchase/receipts_back_receive/detail/index.vue'),
        meta: {
          title: '返仓接收详情',
          active: '/purchase/receipts_back_receive/get_list'
        }
      }
    ]
  },
  {
    path: '/jewelry/receipts_group',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '出入柜' },
    children: [
      {
        path: '/jewelry/receipts_group/detail',
        component: () =>
          import('@/views/jewelry/receipts_group/detail/index.vue'),
        meta: { title: '出入柜详情' }
      }
    ]
  },
  {
    path: '/jewelry/receipts_enter',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '调拨入库' },
    children: [
      {
        path: '/jewelry/receipts_enter/detail',
        component: () =>
          import('@/views/jewelry/receipts_enter/detail/index.vue'),
        meta: { title: '调拨入库详情' }
      }
    ]
  },
  {
    path: '/purchase/receipts_back',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '返仓单' },
    children: [
      {
        path: '/purchase/receipts_back/detail',
        component: () =>
          import('@/views/purchase/receipts_back/detail/index.vue'),
        meta: {
          title: '返仓单详情',
          active: '/purchase/receipts_back/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_store_allocation',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '门店调拨出库' },
    children: [
      {
        path: '/warehouse/receipts_store_allocation/detail',
        component: () =>
          import(
            '@/views/warehouse/receipts_store_allocation/detail/index.vue'
          ),
        meta: {
          title: '门店调拨出库详情',
          active: '/warehouse/receipts_store_allocation/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_store_enter',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '门店调拨入库' },
    children: [
      {
        path: '/warehouse/receipts_store_enter/detail',
        component: () =>
          import('@/views/warehouse/receipts_store_enter/detail/index.vue'),
        meta: {
          title: '门店调拨入库详情',
          active: '/warehouse/receipts_store_enter/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_allocation',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '总部调拨出库' },
    children: [
      {
        path: '/warehouse/receipts_allocation/detail',
        component: () =>
          import('@/views/warehouse/receipts_allocation/detail/index.vue'),
        meta: {
          title: '总部调拨出库详情',
          active: '/warehouse/receipts_allocation/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_enter',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '总部调拨入库' },
    children: [
      {
        path: '/warehouse/receipts_enter/detail',
        component: () =>
          import('@/views/warehouse/receipts_enter/detail/index.vue'),
        meta: {
          title: '总部调拨入库详情',
          active: '/warehouse/receipts_enter/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_store_profit',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '门店报溢' },
    children: [
      {
        path: '/warehouse/receipts_store_profit/detail',
        component: () =>
          import('@/views/warehouse/receipts_store_profit/detail/index.vue'),
        meta: {
          title: '门店报溢详情',
          active: '/warehouse/receipts_store_profit/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_profit',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '总部报溢' },
    children: [
      {
        path: '/warehouse/receipts_profit/detail',
        component: () =>
          import('@/views/warehouse/receipts_profit/detail/index.vue'),
        meta: {
          title: '总部报溢详情',
          active: '/warehouse/receipts_profit/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_tenant_check',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '总部盘点' },
    children: [
      {
        path: '/warehouse/receipts_tenant_check/detail',
        component: () =>
          import('@/views/warehouse/receipts_tenant_check/detail/index.vue'),
        meta: {
          title: '总部盘点信息',
          active: '/warehouse/receipts_tenant_check/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_loss',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '总部报损' },
    children: [
      {
        path: '/warehouse/receipts_loss/detail',
        component: () =>
          import('@/views/warehouse/receipts_loss/detail/index.vue'),
        meta: {
          title: '总部报损详情',
          active: '/warehouse/receipts_loss/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_store_check',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '门店盘点' },
    children: [
      {
        path: '/warehouse/receipts_store_check/detail',
        component: () =>
          import('@/views/warehouse/receipts_store_check/detail/index.vue'),
        meta: {
          title: '门店盘点信息',
          active: '/warehouse/receipts_store_check/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/receipts_store_loss',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '门店报损' },
    children: [
      {
        path: '/warehouse/receipts_store_loss/detail',
        component: () =>
          import('@/views/warehouse/receipts_store_loss/detail/index.vue'),
        meta: {
          title: '门店报损详情',
          active: '/warehouse/receipts_store_loss/get_list'
        }
      }
    ]
  },
  {
    path: '/purchase/receipts_return',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '总部返厂单' },
    children: [
      {
        path: '/purchase/receipts_return/detail',
        component: () =>
          import('@/views/purchase/receipts_return/detail/index.vue'),
        meta: {
          title: '总部返厂单详情',
          active: '/purchase/receipts_return/get_list'
        }
      }
    ]
  },
  {
    path: '/purchase/receipts_supplement',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '首饰补货单' },
    children: [
      {
        path: '/purchase/receipts_supplement/detail',
        component: () =>
          import('@/views/purchase/receipts_supplement/detail/index.vue'),
        meta: {
          title: '首饰补货单详情',
          active: '/purchase/receipts_supplement/get_list'
        }
      }
    ]
  },
  {
    path: '/purchase/receipts_store_return',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '门店返厂单' },
    children: [
      {
        path: '/purchase/receipts_store_return/detail',
        component: () =>
          import('@/views/purchase/receipts_store_return/detail/index.vue'),
        meta: {
          title: '门店返厂单详情',
          active: '/purchase/receipts_store_return/get_list'
        }
      }
    ]
  },
  {
    path: '/jewelry/receipts_loss',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '报损单' },
    children: [
      {
        path: '/jewelry/receipts_loss/detail',
        component: () =>
          import('@/views/jewelry/receipts_loss/detail/index.vue'),
        meta: { title: '报损单详情' }
      }
    ]
  },
  {
    path: '/jewelry/receipts_profit',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '报溢单' },
    children: [
      {
        path: '/jewelry/receipts_profit/detail',
        component: () =>
          import('@/views/jewelry/receipts_profit/detail/index.vue'),
        meta: { title: '报溢单详情' }
      }
    ]
  },
  {
    path: '/warehouse/goods_info',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '首饰库存统计' },
    children: [
      {
        path: '/warehouse/goods_info/detail',
        component: () =>
          import('@/views/warehouse/goods_info/detail/index.vue'),
        meta: {
          title: '首饰库存统计详情',
          active: '/warehouse/goods_info/get_list'
        }
      }
    ]
  },
  {
    path: '/purchase/receipts_alter_goods',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '修改货品' },
    children: [
      {
        path: '/purchase/receipts_alter_goods/detail',
        component: () =>
          import('@/views/purchase/receipts_alter_goods/detail/index.vue'),
        meta: {
          title: '修改货品信息',
          active: '/purchase/receipts_alter_goods/get_list'
        }
      },
      {
        path: '/purchase/receipts_alter_goods/edit',
        component: () =>
          import('@/views/purchase/receipts_alter_goods/edit/index.vue'),
        meta: {
          title: '修改货品信息',
          active: '/purchase/receipts_alter_goods/get_list'
        }
      }
    ]
  },
  {
    path: '/purchase/alter_goods_log',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '修改货品' },
    children: [
      {
        path: '/purchase/alter_goods_log/detail',
        component: () =>
          import('@/views/purchase/alter_goods_log/detail/index.vue'),
        meta: {
          title: '修改货品详情',
          active: '/purchase/alter_goods_log/get_list'
        }
      }
    ]
  },
  {
    path: '/jewelry/receipts_store_check',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '首饰盘点' },
    children: [
      {
        path: '/jewelry/receipts_store_check/detail',
        component: () =>
          import('@/views/jewelry/receipts_store_check/detail/index.vue'),
        meta: {
          title: '首饰盘点（门店）信息',
          active: '/warehouse/receipts_store_check/get_list'
        }
      }
    ]
  },
  {
    path: '/jewelry/receipts_tenant_check',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '首饰盘点' },
    children: [
      {
        path: '/jewelry/receipts_tenant_check/detail',
        component: () =>
          import('@/views/jewelry/receipts_tenant_check/detail/index.vue'),
        meta: {
          title: '首饰盘点（总店）信息',
          active: '/warehouse/receipts_tenant_check/get_list'
        }
      }
    ]
  },
  {
    path: '/usrer/role',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '首饰盘点' },
    children: [
      {
        path: '/user/role/set_permission',
        component: () => import('@/views/user/role/set_permission/index.vue'),
        meta: { title: '设置权限', active: '/user/role/get_list' }
      }
    ]
  },
  {
    path: '/usrer/store_teams',
    component: EleLayout,
    meta: { title: '门店用户管理' },
    children: [
      {
        path: '/user/store_teams/get_list',
        component: () => import('@/views/user/store_teams/get_list/index.vue'),
        meta: { title: '班组管理', active: '/user/store_user/get_list' }
      }
    ]
  },
  {
    path: '/warehouse/group_bound',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '出入柜信息' },
    children: [
      {
        path: '/warehouse/group_bound/detail',
        component: () =>
          import('@/views/warehouse/group_bound/detail/index.vue'),
        meta: {
          title: '出入柜信息详情',
          active: '/warehouse/group_bound/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/group_bound',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '领货' },
    children: [
      {
        path: '/warehouse/group_bound/detail1',
        component: () =>
          import('@/views/warehouse/group_bound/detail1/index.vue'),
        meta: {
          title: '领货详情',
          active: '/warehouse/group_bound/get_list'
        }
      }
    ]
  },
  {
    path: '/warehouse/group_bound',
    component: EleLayout, // 这里要使用EleLayout
    meta: { title: '退货' },
    children: [
      {
        path: '/warehouse/group_bound/detail2',
        component: () =>
          import('@/views/warehouse/group_bound/detail2/index.vue'),
        meta: {
          title: '退货详情',
          active: '/warehouse/group_bound/get_list'
        }
      }
    ]
  },
  {
    path: '/member/receipts_points_send',
    component: EleLayout,
    meta: { title: '手工赠送' },
    children: [
      {
        path: '/member/receipts_points_send/detail',
        component: () =>
          import('@/views/member/receipts_points_send/detail/index.vue'),
        meta: {
          title: '手工赠送详情',
          active: '/member/receipts_points_send/get_list'
        }
      }
    ]
  },
  {
    path: '/member/receipts_points_deduct',
    component: EleLayout,
    meta: { title: '手工扣减' },
    children: [
      {
        path: '/member/receipts_points_deduct/detail',
        component: () =>
          import('@/views/member/receipts_points_deduct/detail/index.vue'),
        meta: {
          title: '手工扣减详情',
          active: '/member/receipts_points_deduct/get_list'
        }
      }
    ]
  },
  {
    path: '/junk/junk_back',
    component: EleLayout,
    meta: { title: '旧货返仓' },
    children: [
      {
        path: '/junk/junk_back/detail',
        component: () => import('@/views/junk/junk_back/detail/index.vue'),
        meta: {
          title: '旧货返仓详情',
          active: '/junk/junk_back/get_list'
        }
      }
    ]
  },
  {
    path: '/junk/junk_out',
    component: EleLayout,
    meta: { title: '旧货出库' },
    children: [
      {
        path: '/junk/junk_out/detail',
        component: () => import('@/views/junk/junk_out/detail/index.vue'),
        meta: {
          title: '旧货出库详情',
          active: '/junk/junk_out/get_list'
        }
      }
    ]
  },
  {
    path: '/member/member',
    component: EleLayout,
    meta: { title: '会员详情' },
    children: [
      {
        path: '/member/member/detail',
        component: () => import('@/views/member/member/detail/index.vue'),
        meta: {
          title: '会员详情',
          active: '/member/member/get_list'
        }
      }
    ]
  },
  {
    path: '/order/refund_manage',
    component: EleLayout,
    meta: { title: '退货单详情' },
    children: [
      {
        path: '/order/refund_manage/detail',
        component: () => import('@/views/order/refund_manage/detail/index.vue'),
        meta: {
          title: '退货单详情',
          active: '/order/refund_manage/get_list'
        }
      }
    ]
  },
  {
    path: '/order/sales_manage',
    component: EleLayout,
    meta: { title: '零售单详情' },
    children: [
      {
        path: '/order/sales_manage/detail',
        component: () => import('@/views/order/sales_manage/detail/index.vue'),
        meta: {
          title: '零售单详情',
          active: '/order/sales_manage/order_list'
        }
      }
    ]
  },
  {
    path: '/order/value_card',
    component: EleLayout,
    meta: { title: '储值卡会员详情' },
    children: [
      {
        path: '/order/value_card/detail',
        component: () => import('@/views/order/value_card/detail/index.vue'),
        meta: {
          title: '储值卡会员详情',
          active: '/order/value_card/get_list'
        }
      }
    ]
  },
  {
    path: '/order/repair_order',
    component: EleLayout,
    meta: { title: '新建维修单' },
    children: [
      {
        path: '/order/repair_order/add',
        component: () => import('@/views/order/repair_order/add/index.vue'),
        meta: {
          title: '新建维修单',
          active: '/order/repair_order/get_list'
        }
      }
    ]
  },
  {
    path: '/order/repair_order',
    component: EleLayout,
    meta: { title: '维修单详情' },
    children: [
      {
        path: '/order/repair_order/detail',
        component: () => import('@/views/order/repair_order/detail/index.vue'),
        meta: {
          title: '维修单详情',
          active: '/order/repair_order/get_list'
        }
      }
    ]
  },
  {
    path: '/order/order_import',
    component: EleLayout,
    meta: { title: '历史销售记录详情' },
    children: [
      {
        path: '/order/order_import/detail',
        component: () => import('@/views/order/order_import/detail/index.vue'),
        meta: {
          title: '历史销售记录详情',
          active: '/order/order_import/get_list'
        }
      }
    ]
  },
  {
    path: '/junk/junk_gram_info',
    component: EleLayout,
    meta: { title: '旧货库存明细详情' },
    children: [
      {
        path: '/junk/junk_gram_info/detail',
        component: () => import('@/views/junk/junk_gram_info/detail/index.vue'),
        meta: {
          title: '旧货库存明细详情',
          active: '/junk/junk_gram_info/get_list'
        }
      }
    ]
  }
];

/**
 * 动态路由
 * @param menus 菜单数据
 * @param homePath 主页地址
 */
export function getMenuRoutes(menus, homePath) {
  const routes = [
    // 用于刷新的路由
    {
      path: REDIRECT_PATH + '/:path(.*)',
      component: RedirectLayout,
      meta: { hideFooter: true }
    }
  ];
  // 路由铺平处理
  eachTreeData(menuToRoutes(menus, getComponent), (route) => {
    routes.push({ ...route, children: void 0 });
  });
  return {
    path: LAYOUT_PATH,
    component: EleLayout,
    redirect: HOME_PATH ?? homePath,
    children: routes
  };
}

/**
 * 解析路由组件
 * @param component 组件名称
 */
function getComponent(component) {
  if (component) {
    const module = modules[`/src/views/${component}.vue`];
    if (!module) {
      return modules[`/src/views/${component}/index.vue`];
    }
    return module;
  }
}
